package com.begye.smartloan.apptest.renzheng.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.isUrl
import org.json.JSONObject

class RenZhengBangKaAdapter(
    var dataList: List<JSONObject>,
    var clickAction: (Int, JSONObject, Boolean) -> Unit
) :
    RecyclerView.Adapter<RenZhengBangKaAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_ren_zheng_bang_ka, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val listItemRenZhengBangKaBangKaTitle: TextView =
            view.findViewById(R.id.listItemRenZhengBangKaBangKaTitle)
        private val listItemRenZhengBangKaBangKaIcon: ImageView =
            view.findViewById(R.id.listItemRenZhengBangKaBangKaIcon)
        private val listItemRenZhengBangKaBangKaDeleteBg: LinearLayout =
            view.findViewById(R.id.listItemRenZhengBangKaBangKaDeleteBg)
        private val listItemRenZhengBangKaBangKaName: TextView =
            view.findViewById(R.id.listItemRenZhengBangKaBangKaName)
        private val listItemRenZhengBangKaBangKaMobile: TextView =
            view.findViewById(R.id.listItemRenZhengBangKaBangKaMobile)

        fun bindData(position: Int, item: JSONObject) {
            listItemRenZhengBangKaBangKaTitle.text = item.optString("title", "")

            val isAdd = item.optBoolean("isAdd", true)
            if (isAdd) {
                listItemRenZhengBangKaBangKaIcon.setImageResource(R.drawable.dres_55)
                listItemRenZhengBangKaBangKaDeleteBg.visibility = View.INVISIBLE
                listItemRenZhengBangKaBangKaName.visibility = View.GONE
                listItemRenZhengBangKaBangKaMobile.text =
                    listItemRenZhengBangKaBangKaMobile.context.getString(R.string.fill_text_13)

                listItemRenZhengBangKaBangKaDeleteBg.setOnClickListener {

                }

                listItemRenZhengBangKaBangKaDeleteBg.setOnClickListener {

                }

                listItemRenZhengBangKaBangKaIcon.setOnClickListener {
                    clickAction.invoke(position, item, true)
                }

                listItemRenZhengBangKaBangKaMobile.setOnClickListener {
                    clickAction.invoke(position, item, true)
                }

            } else {
                listItemRenZhengBangKaBangKaName.visibility = View.VISIBLE
                listItemRenZhengBangKaBangKaDeleteBg.visibility = View.VISIBLE

                listItemRenZhengBangKaBangKaName.text = item.optString("walletDesc", "")
                listItemRenZhengBangKaBangKaMobile.text = item.optString("walletMobile", "")

                listItemRenZhengBangKaBangKaDeleteBg.setOnClickListener {
                    clickAction.invoke(position, item, false)
                }

                listItemRenZhengBangKaBangKaIcon.setOnClickListener {
                    clickAction.invoke(position, item, false)
                }

                listItemRenZhengBangKaBangKaMobile.setOnClickListener {
                    clickAction.invoke(position, item, false)
                }

                val logo = item.optString("walletLogo", "")
                if (logo.isUrl()) {
                    Glide.with(listItemRenZhengBangKaBangKaIcon).load(logo)
                        .into(listItemRenZhengBangKaBangKaIcon)
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }


}